package com.yanqu.road.server.manger.maidian.travelbook;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.travelbook.UserTravelAlbum;
import com.yanqu.road.entity.travelbook.UserTravelCard;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.travelbook.TravelBookModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-11-27
 * @Description : 旅行册快照埋点
 */

public class TravelBookLog {
    public static void trackTravelBookInfoSnapshot(GamePlayer player) {
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) {
                return;
            }
            TravelBookModule module = player.getModule(TravelBookModule.class);

            int cardCnt = 0;
            List<Map<String, Object>> cardArr = new ArrayList<>();
            List<Map<String, Object>> albumArr = new ArrayList<>();
            List<Map<String, Object>> patronArr = new ArrayList<>();
            for (UserTravelAlbum album : module.getAlbumMap().values()) {
                List<UserTravelCard> cards = album.getCards();
                for (UserTravelCard card : cards) {
                    if (card.getLevel() > 0) {
                        cardCnt++;
                        Map<String, Object> cardMap = new HashMap<>();
                        cardMap.put(EventDataConstants.TravelBook.CARD_ID, card.getCardId());
                        cardMap.put(EventDataConstants.TravelBook.CARD_LEVEL, card.getLevel());
                        cardArr.add(cardMap);
                        if (card.getPatronId() != 0) {
                            Map<String, Object> patronMap = new HashMap<>();
                            patronMap.put(EventDataConstants.TravelBook.CARD_ID, card.getCardId());
                            patronMap.put(EventDataConstants.TravelBook.PATRON_ID, card.getPatronId());
                            patronArr.add(patronMap);
                        }
                    }
                }
                Map<String, Object> albumMap = new HashMap<>();
                albumMap.put(EventDataConstants.TravelBook.BOOK_ID, album.getAlbumId());
                albumMap.put(EventDataConstants.TravelBook.BOOK_LEVEL, album.getLevel());
                albumArr.add(albumMap);
            }

            if (cardCnt > 0) {
                DefaultEventTraceAction action = new DefaultEventTraceAction(EventDataConstants.TravelBook.TRAVEL_INFO, player);
                action.build(EventDataConstants.TravelBook.NOW_NUMBER, cardCnt);
                action.build(EventDataConstants.TravelBook.TRAVEL_CARD, cardArr);
                action.build(EventDataConstants.TravelBook.TRAVEL_BOOK, albumArr);
                action.build(EventDataConstants.TravelBook.TRAVEL_CHOO, patronArr);
                action.trace();
            }
        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }
    }
}
